package com.geekaca.news.controller.admin;

import cn.hutool.crypto.SecureUtil;
import com.geekaca.news.domain.AdminUser;
import com.geekaca.news.service.AdminUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class ProfileController {
    @Autowired
    private AdminUserService userService;

    @RequestMapping("/profile")
    public String profile(HttpServletRequest request, HttpSession session) {
        request.setAttribute("path", "profile");
        String userNickName = (String) session.getAttribute("loginUser");
        Integer loginUserId = (Integer) session.getAttribute("loginUserId");
        String loginUserName = (String) session.getAttribute("loginUserName");
        request.setAttribute("loginUserName", loginUserName);
        request.setAttribute("nickName", userNickName);
        request.setAttribute("loginUserId", loginUserId);
        return "admin/profile";
    }

    @PostMapping("/profile/name")
    @ResponseBody
    public Object profileName(@RequestParam("loginUserName") String loginUserName,
                              @RequestParam("nickName") String nickName,
                              HttpSession session) {
        String r = "";
        AdminUser adminUser = new AdminUser();
        adminUser.setLoginUserName(loginUserName);
        adminUser.setNickName(nickName);
        adminUser.setAdminUserId((Integer) session.getAttribute("loginUserId"));
        int i = userService.updateProfile(adminUser);
        if (i > 0) {
            session.setAttribute("loginUserName", loginUserName);
            session.setAttribute("loginUser", nickName);
            r = "success";
        }
        return r;
    }

    @PostMapping("/profile/password")
    @ResponseBody
    public Object profilePasswd(@RequestParam("originalPassword") String originalPassword,
                                @RequestParam("newPassword") String newPassword,
                                HttpSession session) {
        String r = "";
        String md5pass = SecureUtil.md5(originalPassword);
        Integer loginUserId = (Integer) session.getAttribute("loginUserId");
        System.out.println("loginUserId: "+loginUserId);
        int userCount = userService.getPasswd(loginUserId, md5pass);
        if (userCount > 0) {
            AdminUser adminUser = new AdminUser();
            adminUser.setAdminUserId(loginUserId);
            adminUser.setLoginPassword(SecureUtil.md5(newPassword));
            int i = userService.updateProfile(adminUser);
            if (i > 0) {
                r = "success";
            }
        }
        return r;
    }


}
